<?php
/*
Plugin Name: Custom Widgets
Description: Add some custom widgets.
Author: Manuel EVENO
Version: 0.1
Author URI: http://mektoub.free.fr/
*/

function enhance_wp_tag_cloud($content) {
	return '<p align="center">' . $content . '</p>';
}

add_filter( 'wp_tag_cloud', 'enhance_wp_tag_cloud');

function widget_linksByCategory($args, $number = 1) {
	extract($args);
	$optionsArray = get_option("widget_linksByCategory");
	$options = $optionsArray[$number];

	echo $before_widget;
	echo $before_title . $options['title'] . $after_title . "<ul>";	
	wp_list_bookmarks(
		array(
			'categorize' => 0,
			'category' => $options['categoryId'],
			'show_images' => 0,
			'show_description' => 0,
			'category_orderby' => 'name',
			'show_rating' => 0,
			'show_updated' => 0,
			'before' => '<li>',
			'after' => '</li>',
			'title_li' => '',
			'title_before' => '',
			'title_after' => ''
		)
	);
	echo "</ul>" . $after_widget;
}

function widget_linksByCategory_configure($number = 1) {
	$optionsArray = get_option("widget_linksByCategory");

	if (!is_array( $optionsArray )) {
		$optionsArray = array();
		//update_option("widget_linksByCategory", $optionsArray);
	}
	$options = $optionsArray[$number];
	if (!is_array( $options )) {
		$options = array(
				'title' => 'Links',
				'categoryId' => ''
			);
	}

	if ($_POST["linksByCategory-Submit-$number"]) {
		echo "Title: ".$_POST["linksByCategory-widgetTitle-$number"].", categoryId: ".$_POST["linksByCategory-categoryId-$number"]."<br/>";
		$options['title'] = htmlspecialchars($_POST["linksByCategory-widgetTitle-$number"]);
		$options['categoryId'] = htmlspecialchars($_POST["linksByCategory-categoryId-$number"]);
		$optionsArray[$number] = $options;
		update_option("widget_linksByCategory", $optionsArray);
	}

?>
	<input type="hidden" id="linksByCategory-Submit-<?php echo $number; ?>"
		name="linksByCategory-Submit-<?php echo $number; ?>" value="<?php echo $number; ?>" />
	<p>
		<label for="linksByCategory-widgetTitle-<?php echo $number; ?>">Widget Title: </label>
		<input type="text" id="linksByCategory-widgetTitle-<?php echo $number; ?>"
			name="linksByCategory-widgetTitle-<?php echo $number; ?>" value="<?php echo $options['title'];?>" />
	</p>
	<p>
		<label for="linksByCategory-categoryId-<?php echo $number; ?>">Category Id: </label>
		<input type="text" id="linksByCategory-categoryId-<?php echo $number; ?>"
			name="linksByCategory-categoryId-<?php echo $number; ?>" value="<?php echo $options['categoryId']; ?>" />
	</p>
<?php
}

function linksByCategory_init()
{
	$options = array(
		'classname' => 'widget_linksByCategory',
		'description' => 'Adds a widget that show links for a specified category'
	);
	for ($i = 1; $i <= 3; $i++) {
		wp_register_sidebar_widget('linksByCategory'.$i, __('Links By Category'), 'widget_linksByCategory', $options, $i);
		wp_register_widget_control('linksByCategory'.$i, __('Links By Category'), 'widget_linksByCategory_configure', array(), $i);
	}
}

add_action('plugins_loaded', 'linksByCategory_init');


function widget_themeSwitcher($args) {
	extract($args);
	$options = get_option("widget_themeSwitcher");
	echo $before_widget;
	echo $before_title . $options['title'] . $after_title . '<ul>';
	wp_theme_switcher($options['list_format']);
	echo '</ul>' . $after_widget;
}

function widget_themeSwitcher_configure() {
	$options = get_option("widget_themeSwitcher");

	if (!is_array( $options )) {
		$options = array(
			'title' => 'Theme Switcher',
			'list_format' => 'dropdown'
		);
	}

	if ($_POST['themeSwitcher-Submit']) {
		$options['title'] = htmlspecialchars($_POST['themeSwitcher-WidgetTitle']);
		$options['list_format'] = htmlspecialchars($_POST['themeSwitcher-ListFormat']);
		update_option("widget_themeSwitcher", $options);
	}

	$dropdown_selected = '';
	$text_selected = '';
	if ($options['list_format'] == 'dropdown') {
		$dropdown_selected = 'selected';
	} else {
		$text_selected = 'selected';
	}

?>
	<input type="hidden" id="themeSwitcher-Submit" name="themeSwitcher-Submit" value="1" />
	<p>
		<label for="themeSwitcher-WidgetTitle">Widget Title: </label>
		<input type="text" id="themeSwitcher-WidgetTitle" name="themeSwitcher-WidgetTitle" value="<?php echo $options['title'];?>" />
	</p>
	<p>
		<label for="themeSwitcher-ListFormat">List Format: </label>
		<select id="themeSwitcher-ListFormat" name="themeSwitcher-ListFormat" size="1">
			<option value="dropdown" <?php echo $dropdown_selected ?>><?php echo __('Dropdown'); ?></option>
			<option value="text" <?php echo $text_selected ?>><?php echo __('Text'); ?></option>
		</select>
	</p>
<?php
}

function themeSwitcher_init()
{
	$options = array(
		'classname' => 'widget_themeSwicther',
		'description' => 'Adds a control to swicth between installed theme'
	);
	wp_register_sidebar_widget('themeSwitcher', __('Theme Switcher'), 'widget_themeSwitcher', $options);
	wp_register_widget_control('themeSwitcher', __('Theme Switcher'), 'widget_themeSwitcher_configure');
}

if (function_exists('wp_theme_switcher')) {
    add_action("plugins_loaded", "themeSwitcher_init");
}

?>
